﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLCHBANGIAY.DATA;
using QLCHBANGIAY.BUSINESS;

namespace QuanLyBanGiay
{
    public partial class Phanloaisanpham : System.Web.UI.Page
    {
        string categoryid = "";
        string currentpage = "1";
        List<Product> productselected = new List<Product>();
     
        protected void Page_Load(object sender, EventArgs e)
        {


            if (Page.Request.QueryString["tag"] != null) // 
            {
               // categoryid = Page.RouteData.Values["title"].ToString();
                categoryid = Page.Request.QueryString["tag"];
            }
            if (Page.RouteData.Values["currentpage2"] != null)
            {
                currentpage = Page.RouteData.Values["currentpage2"].ToString();
                bindata(currentpage, "12");
            }


            bindata(currentpage, "12");
            show1();
            
        }
        public void bindata(string currentpage, string pagesize)
        {
            //list = ProductServices.Product_Paging(currentpage, pagesize, "", "");
            //  List<Product> products = new List<Product>();
            ////  products = ProductServices.Product_GetByAll();
            //  List<Product> list = new List<Product>();
            //  list = ProductServices.Product_Paging(currentpage, pagesize, "", "");
            List<Product> list = new List<Product>();
            List<Category> listca = new List<Category>();
            if (categoryid != "")
            {
                listca = CategoryServices.Category_GetByTop("1", " Tag='" + categoryid + "'", "");
             //   list = ProductServices.Product_Paging(currentpage, pagesize, " categogyid='" + listca[0].Id + "'", "");
                //  list = ProductServices.Product_GetByTop("", " CatId='" + listca[0].Id + "'", "");
                list = ProductServices.Product_Paging(currentpage, pagesize, "", " Id desc");
            }
            else
            {
                listca = CategoryServices.Category_GetByTop("1", "", " Id desc");
                list = ProductServices.Product_Paging(currentpage, pagesize, "", " Id desc");
            }
            listca.Clear();
            listca = null;
            string Chuoi = "";
            String chuoi = "";
            for (int i = 0; i < list.Count; i++)
            {
                chuoi += " <div class='product_box'>";
                chuoi += "<a href='productdetail.html'><img  src='" + list[i].Image + "' alt='Shoes 1' width='200' heigh='200'></a>";
                chuoi += "<p>" + list[i].Content + "</p>";
                chuoi += "<p class='product_price'>$ " + list[i].Price + "</p>";
                chuoi += " <a href='index.html/productid='" + list[i].Id + " class='addtocart'></a>";
                //href='/index/.htm/page=" + categoryid + "
                //cho id vô
                chuoi += " <a href='ChitietSanpham.aspx?id="+list[i].Id+"' class='detail'></a>";
                chuoi += " </div>";
                //
                //
                //  if (i == 3 || i == 7 || i == 11)
                //      chuoi += "<div class='cleaner'></div>";
                ////
                //
                //=

            }
            ltrProduct.Text = chuoi;
            list.Clear();
            list = null;
        }
        void show1()
        {
            string chuoi = "";
            List<Product> listpage = new List<Product>();
            List<Category> listcate = new List<Category>();
            if (categoryid != "")
            {
                listcate = CategoryServices.Category_GetByTop("1", " Tag='" + categoryid + "'", "");
                listpage = ProductServices.Product_GetByTop("", " categoryid='" + listcate[0].Id + "'", "");
                //  lblheader.Text = listcate[0].Name;
            }
            else
            {
                listpage = ProductServices.Product_GetByTop("", " ", " Id desc");
                //  lblheader.Text = "Sản phẩm";
            }
            chuoi += "<ul>";
            if (listpage.Count > 12)
            {
                if ((listpage.Count) % 12 == 0)
                {
                    if (Int32.Parse(currentpage) == 1)
                    {
                        chuoi += "<li><a  class='li_first' href='/index.htm/page=1'>" + "Prev" + "</a></li>";
                        //  chuoi += "<li><a  class='li_first' href='/Products/" + categoryid + ".htm/page=1'>" + "Prev" + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a class='li_first' href='/index/.htm/page=" + categoryid + "" + (Int32.Parse(currentpage) - 1).ToString() + "'>" + "Prev" + "</a></li>";
                    }
                    for (int i = 1; i <= (listpage.Count / 12); i++)
                    {
                        if (i.ToString().Equals(currentpage))
                        {
                            chuoi += "<li><a class='selected tab' href='/index.htm/page=" + i + "'>" + i + "</a></li>";
                        }
                        else
                        {
                            chuoi += "<li><a class='tab' href='/index.htm/page=" + i + "'>" + i + "</a></li>";
                        }
                    }
                    if (Int32.Parse(currentpage) == ((listpage.Count / 12) + 1))
                    {
                        chuoi += "<li><a class='li_last' href='/index.htm/page=" + currentpage + "'>" + "Next" + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a class='li_last' href='/index.htm/page=" + (Int32.Parse(currentpage) + 1).ToString() + "'>" + "Next" + "</a></li>";
                    }
                }
                else
                {
                    if (Int32.Parse(currentpage) == 1)
                    {
                        chuoi += "<li><a class='li_first' href='/index.htm/page=1'>" + "Prev" + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a class='li_first' href='/index.htm/page=" + (Int32.Parse(currentpage) - 1).ToString() + "'>" + "Prev" + "</a></li>";
                    }

                    for (int i = 1; i <= (listpage.Count / 12) + 1; i++)
                    {
                        if (i.ToString().Equals(currentpage))
                        {
                            chuoi += "<li><a class='selected tab' href='/index.htm/page=" + i + "'>" + i + "</a></li>";
                        }
                        else
                        {
                            chuoi += "<li><a class='tab' href='/index.htm/page=" + i + "'>" + i + "</a></li>";
                        }
                    }
                    if (Int32.Parse(currentpage) == ((listpage.Count / 12) + 1))
                    {
                        chuoi += "<li><a class='li_last' href='/index.htm/page=" + currentpage + "'>" + "Next" + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a class='li_last' href='/index.htm/page=" + (Int32.Parse(currentpage) + 1).ToString() + "'>" + "Next" + "</a></li>";
                    }
                }

                //BindData(currentpage, "3");
            }
            else
            {
                string Chuoipro = "";
                if (listpage.Count > 0)
                {
                    for (int i = 0; i < listpage.Count; i++)
                    {
                        Chuoipro += " <div class='product_box'>";
                        Chuoipro += "<a href='productdetail.html'><img src='" + listpage[i].Image + "' alt='Shoes 1'></a>";
                        Chuoipro += "<p>" + listpage[i].Content + "</p>";
                        Chuoipro += "<p class='product_price'>$ " + listpage[i].Price + "</p>";
                        Chuoipro += " <a href='/productid='" + listpage[i].Id + " class='addtocart'></a>";
                        // <a href="./index.jsp?shop=<%=product.getId()%>" 
                        Chuoipro += " <a href='~/ChitietSanpham.aspx?id=<"+listpage[i].Id+">' class='detail'></a>";
                        Chuoipro += " </div>";
                    }
                }
                ltrProduct.Text = Chuoipro;
                listpage.Clear();
                listpage = null;
                listcate.Clear();
                listcate = null;
            }
            chuoi += "</ul>";
            ltrpaging.Text = chuoi;

        }

        protected string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }
    }
}